<template>
  <div>
    <div style="padding: 10px 0">
      <el-input clearable placeholder="请输入关键词" style="width: 260px; margin-right: 10px"
                v-model="GetUserListDTO.keyWord"></el-input>
      <el-button type="info" plain style="margin-left: 10px" @click="load" >搜索</el-button>
      <el-button type="warning" plain style="margin-left: 10px" @click="reset">重置</el-button>
      <el-button plain style="margin-left: 10px" type="danger" @click="delBatch">批量删除</el-button>
    </div>
    <el-table align="center" :data="tableData" strip @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column prop="id" label="ID" width="80" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="avatar" label="头像" width="140" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="username" label="用户名" width="140" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="phone" label="电话" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="email" label="邮箱" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="gender" label="性别" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="modifiedUser" label="最后修改用户" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="modifiedTime" label="最后修改时间" show-overflow-tooltip>
      </el-table-column>

      <el-table-column width="260px">
        <template v-slot="scope">
          <el-button type="success" plain style="margin-left: 10px" @click="handleEdit(scope.row)">编辑</el-button>
          <el-popconfirm
              style="margin-left: 5px"
              confirm-button-text='好的'
              cancel-button-text='不用了'
              icon="el-icon-info"
              icon-color="red"
              title="这是一段内容确定删除吗？"
              @confirm="del(scope.row.id)"
          >
            <el-button type="danger" plain style="margin-left: 10px" slot="reference">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination" style="margin-left: 500px">
      <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="GetUserListDTO.pageIndex"
          :page-sizes="[2, 5, 10]"
          :page-size="GetUserListDTO.pageSize"
          layout="total, prev, pager, next"
          :total="total">
      </el-pagination>
    </div>

    <el-dialog title="用户信息" :visible.sync="dialogFormVisible" width="30%">
      <el-form label-width="60px">
        <el-form-item label="用户名">
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱地址">
          <el-input v-model="form.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-input v-model="form.gender" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button plain style="margin-left: 10px" @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" plain style="margin-left: 10px" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "User",
  data(){
    return{
      tableData: [],
      GetUserListDTO:{
        pageIndex: 1,
        pageSize: 5,
        keyWord: "",
      },
      total: 0,
      multipleSelection: [],
      dialogFormVisible: false,//表单对话框（新增用户），false-不展示
      form: {
        id:"",
        username: "",
        phone: "",
        email: "",
        gender: ""
      }
    }
  },
  methods:{
    load() {
      this.$request.post("/user/list", this.GetUserListDTO).then(res => {
        this.tableData = res.data.data//此处建议比照控制台元素名称
        this.total = res.data.count
      })
    },
    reset() {
      this.keyWord = ""
      this.load()
    },
    handleAdd() {
      this.dialogFormVisible = true
      this.form = {}
    },
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row))
      this.dialogFormVisible = true
    },
    del(id) {//有问题
      console.log(id)
      this.$request.put("/user/del/" + id).then(res => {
        if (res) {
          this.$message.success("用户信息删除成功!")
        } else {
          this.$message.error("用户信息删除失败!")
        }
        this.load()
      })
    },
    save() {
      console.log(this.form)
      this.$request.put("/user/update2", this.form).then(res => {
        if (res) {
          this.$message.success("用户修改成功!")
          this.load()
        } else {
          this.$message.error("用户修改失败!")
        }
        this.dialogFormVisible = false
      })
    },
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    delBatch() {
      let ids = this.multipleSelection.map(v => v.id) //将对象数组转为id数组
      this.$request.post("/user/delBatch", ids).then(res => {
        if (res) {
          this.$message.success("批量删除成功!")
          this.load()
        } else {
          this.$message.error("批量删除失败!")
        }
      })
    },
    handleSizeChange(pageSize) {
      console.log(`每页 ${pageSize} 条`);
      this.pageSize = pageSize;
      this.load();
    },
    handleCurrentChange(pageNum) {
      console.log(`当前页: ${pageNum}`);
      this.pageNum = pageNum;
      this.load();
    }
  },
  created() {
    this.load();
  },
}
</script>

<style scoped>

</style>